package com.zm.zmgz.utils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: cjw
 * @Date: 2020/05/21/19:13
 * @Description:
 */
public class BigDecimalUtils {

    
    
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        for (String s : list) {
            System.err.println(s);
        }
    }

    
    /***
    * @Description: BigDecimal转化为Double
    * @Param: 
    * @return: 
    * @Author: cjw
    * @Date: 2020-06-02 11:57:39
    */
    public static Double getDoubleByBigDecimal(BigDecimal decimal){
        if(Objects.isNull(decimal))
            return null;
        return decimal.doubleValue();
    }

    /***
    * @Description: 乘法
    * @Param: 
    * @return: 
    * @Author: cjw
    * @Date: 2020-06-01 21:32:06
    */
    public static BigDecimal multiply(BigDecimal bigDecimal,BigDecimal bigDecimal2) {
        if(Objects.isNull(bigDecimal) || Objects.isNull(bigDecimal2))
            return null;
        return bigDecimal.multiply(bigDecimal2);
    }
    
    /***
    * @Description: double转化为BigDecimal
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-06-01 21:28:14
    */
    public static BigDecimal getBigDecimalByDouble(Double value) {
        if (Objects.isNull(value))
            return new BigDecimal(0);
        return new BigDecimal(value);
    }

    /***
     * @Description: 从数值型字符串得到BigDecimal对象
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-26 12:21:56
     */
    public static BigDecimal getBigDecimalByString(String value) {
        if (Objects.isNull(value))
            return new BigDecimal(0);
        double v = Double.parseDouble(value);
        return new BigDecimal(v);
    }

    /***
     * @Description: 求和
     * 首先BigDecimal类型求和需要用到特定的方法(要求求和对象不能为空)
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-21 19:14:58
     */
    public static BigDecimal add(BigDecimal decimal, BigDecimal decimal2) {
        decimal = nullToZero(decimal);
        decimal2 = nullToZero(decimal2);
        return decimal.add(decimal2);
    }

    /***
     * @Description: 求和
     * 首先BigDecimal类型求和需要用到特定的方法(要求求和对象不能为空)
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-21 19:14:58
     */
    public static BigDecimal adds(BigDecimal... decimal) {
        BigDecimal bigDecimal = new BigDecimal(0);
        for (BigDecimal bigDecimal1 : decimal) {
            bigDecimal1 = nullToZero(bigDecimal1);
            bigDecimal = bigDecimal.add(bigDecimal1);
        }
        return bigDecimal;

    }


    /***
     * @Description: null转0
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-21 19:27:50
     */
    public static BigDecimal nullToZero(BigDecimal decimal) {
        BigDecimal bigDecimal = new BigDecimal(0);
        if (Objects.isNull(decimal))
            return bigDecimal;
        else
            return decimal;
    }
}
